#include	"Texture.h"
#include	"IImage.h"
#include	<OpenGL/GLException.h>
#include	<windows.h>
#include	<gl/gl.h>

Texture::Texture()
	:	m_name(0)
{
}

Texture::Texture(const IImage & image)
	:	m_name(0)
{
	Create(image);
}

Texture::~Texture()
{
	Destroy();
}

void Texture::Create(const IImage & image)
{
	if(image.Valid() != 0)
	{
		glGenTextures(1, &m_name);
		GLenum code = glGetError();
		if(code != GL_NO_ERROR)
		{
			throw GLException(code);
		}
		image.RegisterInOpenGL(m_name);
		//glBindTexture(GL_TEXTURE_2D, m_name);
		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &(m_size[0]));
		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &(m_size[1]));
	}
}

void Texture::Destroy()
{
	if(m_name != 0)
	{
		glDeleteTextures(1, &m_name);
	}
}

unsigned int Texture::Name() const
{
	return m_name;
}

const Vector2i & Texture::GetSize() const
{
	return m_size;
}

